package com.cecsmsserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.entity.HealthReminder;
import com.cecsmsserve.mapper.HealthReminderMapper;
import com.cecsmsserve.service.HealthReminderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class HealthReminderServiceImpl extends ServiceImpl<HealthReminderMapper, HealthReminder> implements HealthReminderService {

    @Autowired
    private HealthReminderMapper healthReminderMapper;

    @Override
    public boolean addReminder(HealthReminder healthReminder) {
        if (healthReminder.getCreateTime() == null) {
            healthReminder.setCreateTime(LocalDateTime.now());
        }
        
        if (healthReminder.getIsActive() == null) {
            healthReminder.setIsActive(true);
        }
        
        return save(healthReminder);
    }

    @Override
    public Page<HealthReminder> getRemindersByUserIdWithPage(int pageNum, int pageSize, Integer userId) {
        return healthReminderMapper.getRemindersByUserIdWithPage(new Page<>(pageNum, pageSize), userId);
    }

    @Override
    public boolean updateReminder(HealthReminder healthReminder) {
        if (healthReminder.getId() == null || healthReminder.getUserId() == null) {
            return false;
        }
        
        LambdaQueryWrapper<HealthReminder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HealthReminder::getId, healthReminder.getId())
                .eq(HealthReminder::getUserId, healthReminder.getUserId());
        
        return update(healthReminder, wrapper);
    }

    @Override
    public boolean deleteReminder(Integer id, Integer userId) {
        if (id == null || userId == null) {
            return false;
        }
        
        LambdaQueryWrapper<HealthReminder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HealthReminder::getId, id)
                .eq(HealthReminder::getUserId, userId);
        
        return remove(wrapper);
    }

    @Override
    public boolean updateReminderStatus(Integer id, Integer userId, Boolean isActive) {
        if (id == null || userId == null || isActive == null) {
            return false;
        }
        
        HealthReminder reminder = new HealthReminder();
        reminder.setIsActive(isActive);
        
        LambdaQueryWrapper<HealthReminder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HealthReminder::getId, id)
                .eq(HealthReminder::getUserId, userId);
        
        return update(reminder, wrapper);
    }
} 